/*
 *  Copyright 2004 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *
 */
#include <std.h>
#include <csl_cache.h>
#include <fvid.h>
#include <csl_dat.h>
#include "colorbar.h"

/* static function declaration */
static void generateColorBar(Uint8* y, Uint8* cb, Uint8* cr, Int lineSize);

#define WHITE_Y        180
#define WHITE_CB       128
#define WHITE_CR       128

#define YELLOW_Y       162
#define YELLOW_CB      44
#define YELLOW_CR      142

#define CYAN_Y         131
#define CYAN_CB        156
#define CYAN_CR        44

#define GREEN_Y        112
#define GREEN_CB       72
#define GREEN_CR       58

#define MAGENTA_Y       84
#define MAGENTA_CB     184
#define MAGENTA_CR     198

#define RED_Y          65
#define RED_CB         100
#define RED_CR         212

#define BLUE_Y          35
#define BLUE_CB        212
#define BLUE_CR        114

#define BLACK_Y        16
#define BLACK_CB       129
#define BLACK_CR       129

/* defines arrays to hold a line of image for color bar generating */
#pragma DATA_ALIGN(imgLineY, 8);
#pragma DATA_ALIGN(imgLineCb, 8);
#pragma DATA_ALIGN(imgLineCr, 8);

static Uint8 imgLineY[1920 * 3];
static Uint8 imgLineCb[960 * 3];
static Uint8 imgLineCr[960 * 3];

/*
 * ======== fillFrmBuf ========
 * This function fill a frame with color bar of Y/Cb/Cr format.
 */

void fillFrmBuf(FVID_IFrame* frame, Int lineSz, Int numLines, Int offset )
{
    Int i;
    static Int init = 0;
    Int id;
    if(!init) {
        generateColorBar(imgLineY, imgLineCb, imgLineCr, lineSz);
        CACHE_clean(CACHE_L2ALL, NULL, NULL);
        init = 1;
    }
    for(i = 0; i < numLines; i ++) {
        DAT_copy(imgLineY + offset * 2, frame->y1 + lineSz * i, lineSz);
        DAT_copy(imgLineCb + offset, frame->cb1 + (lineSz >> 1) * i,
            (lineSz >> 1));
        id = DAT_copy(imgLineCr + offset, frame->cr1+(lineSz >> 1) * i,
            (lineSz >> 1));
    }
    DAT_wait(id);
}

/*
 * ======== generateColorBar ========
 * This function generates a line of color bar with y/cb/cr format.
 */

static void generateColorBar(Uint8* y, Uint8* cb, Uint8* cr, Int lineSize)
{
    Int i;
    Int fillSize = lineSize >> 3;
    Int k;

    if(fillSize & 0x1) {
        fillSize ++;
    }
    /* white bar */
    for(k = 0; k < 3; k ++) {
        for(i = 0; i<(fillSize >> 1); i++) {

            y[2 * i] = WHITE_Y;
            y[2 * i + 1] = WHITE_Y;
            cb[i] = WHITE_CR;
            cr[i] = WHITE_CB;

            y[fillSize * 1 + 2 * i] = YELLOW_Y;
            y[fillSize * 1 + 2 * i + 1] = YELLOW_Y;
            cb[(fillSize >> 1) * 1 + i] = YELLOW_CB;
            cr[(fillSize >> 1) * 1 + i] = YELLOW_CR;

            y[fillSize * 2 + 2 * i] = CYAN_Y;
            y[fillSize * 2 + 2 * i + 1] = CYAN_Y;
            cb[(fillSize >> 1) * 2 + i] = CYAN_CB;
            cr[(fillSize >> 1) * 2 + i] = CYAN_CR;

            y[fillSize * 3 + 2 * i] = GREEN_Y;
            y[fillSize * 3 + 2 * i + 1] = GREEN_Y;
            cb[(fillSize >> 1) * 3 + i] = GREEN_CB;
            cr[(fillSize >> 1) * 3 + i] = GREEN_CR;

            y[fillSize * 4 + 2 * i] = MAGENTA_Y;
            y[fillSize * 4 + 2 * i + 1] = MAGENTA_Y;
            cb[(fillSize >> 1) * 4 + i] = MAGENTA_CB;
            cr[(fillSize >> 1) * 4 + i] = MAGENTA_CR;

            y[fillSize * 5 + 2 * i] = RED_Y;
            y[fillSize * 5 + 2 * i + 1] = RED_Y;
            cb[(fillSize >> 1) * 5 + i] = RED_CB;
            cr[(fillSize >> 1) * 5 + i] = RED_CR;

            y[fillSize * 6 + 2 * i] = BLUE_Y;
            y[fillSize * 6 + 2 * i + 1] = BLUE_Y;
            cb[(fillSize >> 1) * 6 + i] = BLUE_CB;
            cr[(fillSize >> 1) * 6 + i] = BLUE_CR;

            y[fillSize * 7 + 2 * i] = BLACK_Y;
            y[fillSize * 7 + 2 * i + 1] = BLACK_Y;
            cb[(fillSize >> 1) * 7 + i] = BLACK_CB;
            cr[(fillSize >> 1) * 7 + i] = BLACK_CR;
        }
        y  += lineSize;
        cb += lineSize >> 1;
        cr += lineSize >> 1;
    }
}
